package com.liu.frame.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liu.frame.R;

/**
 * Created by 刘智强 on 2017/2/8.
 *
 * 标题栏
 */

public class TitleBar extends FrameLayout {

    private LinearLayout linearLayout_titlebar;//整个titlebar
    private  LinearLayout linearLayout_left;//左边的button
    private ImageView  iv_left;//左边图片
    private TextView tv_title;//中间的标题
    private TextView tv_right;//右边的标题
    private  ImageView iv_right;//右边的图片
    private View view_line;//titlebar下方的线
    private RelativeLayout relativeLayout_right;//右侧按钮

    private  boolean isTransparent;//是否设置标题栏透明 true 透明 false 不透明

    public boolean isTransparent() {
        return isTransparent;
    }

    public void setTransparent(boolean transparent) {
        isTransparent = transparent;
    }

    public TitleBar(Context context) {
        super(context);
        setView(context);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setView(context);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setView(context);
    }

    public  void setView(Context context){
        LayoutInflater.from(context).inflate(R.layout.titlebar,this);
        linearLayout_titlebar= (LinearLayout) findViewById(R.id.ly_background);
        linearLayout_left= (LinearLayout) findViewById(R.id.ly_left);
        iv_left= (ImageView) findViewById(R.id.iv_left);
        tv_title= (TextView) findViewById(R.id.tv_title);
        tv_right= (TextView) findViewById(R.id.tv_right);
        iv_right= (ImageView) findViewById(R.id.iv_right);
        view_line=findViewById(R.id.view_line);
        relativeLayout_right= (RelativeLayout) findViewById(R.id.rl_right);

    }

    /**
     * 是否设置标题栏透明  true 透明 false 不透明
     * @param isTransparent
     */

    public  void setTitleBarTransparent(boolean isTransparent){
        if (isTransparent){
            linearLayout_titlebar.getBackground().setAlpha(0);//透明

        }else {
            linearLayout_titlebar.getBackground().setAlpha(255);//不透明

        }

    }

    /**
     * 设置标题栏颜色
     * @param colorId
     */

    public  void setTitleBarBackGroundColor(int colorId){
        if (colorId!=0){
            linearLayout_titlebar.setBackgroundColor(colorId);

        }

    }

    /**
     * 设置左边按钮的背景图片
     * @param resourceId
     */

    public void setLeftImageResource(int resourceId){
        if (resourceId!=0){
            iv_left.setBackgroundResource(resourceId);
        }
    }

    /**
     * 设置中间标题内容
     * @param title
     */

    public  void setTitle(String title){
        if (tv_title!=null){
            tv_title.setText(title);
        }

    }

    /**
     * 设置右侧标题内容
     * @param text
     */

    public  void setRightText(String  text){
        if (tv_right!=null){
            tv_right.setText(text);
        }

    }

    /**
     * 设置右侧textview是否显示
     * @param visibily
     */

    public  void setRightTextViewVisible(boolean visibily){
        if (tv_right!=null){
            if (visibily){
                tv_right.setVisibility(View.VISIBLE);
            }else {
                tv_right.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 设置右侧imageview是否显示
     * @param visible
     */
    public  void setRightImagviewVisible(boolean visible){
        if (iv_right!=null){
            if (visible){
                iv_right.setVisibility(View.VISIBLE);
            }else {
            iv_right.setVisibility(View.GONE);}
        }
    }

    /**
     *  设置右侧背景图片
     * @param resourceId
     */

    public  void setRightResource(int resourceId){
        if (iv_right!=null){
            iv_right.setBackgroundResource(resourceId);
        }

    }

    /**
     * 左侧按钮设置点击监听
     * @param listener
     */

    public  void setLeftOnClickListener(OnClickListener listener){
        if (linearLayout_left!=null){
            linearLayout_left.setOnClickListener(listener);

        }

    }

    /**
     * 右侧按钮设置点击监听
     * @param listener
     */

    public  void setRightTextViewOnClickListener(OnClickListener listener){
        if (relativeLayout_right!=null){
            relativeLayout_right.setOnClickListener(listener);
        }

    }


}
